
var PacketFactory = function(stage){
    var _pool = new Array();
    var _stage = stage;
    
    this.getPacketWith = function(x,y,health,speed,emitter){
        if(!_pool[emitter.getType()]){
            _pool[emitter.getType()] = new Array();
        }
        //dépile le dernier paquet de ce type présent en cache
        var packet = _pool[emitter.getType()].pop();
        
        //si le paquet existe, on le reconfigure
        if(packet){
            packet._reinit(x,y,health,speed,emitter);
        } else {
            //sinon on le créer
            packet = new Packet(x, y, health, speed, emitter, _stage);
            //sans oublier de demander à être averti à la destruction
            packet.setCallbackOnDestroy(_callbackOnDestroy);
        }
        
        return packet;
    }
    
    //callback de destruction des paquets. Ajoute le paquet au cache
    var _callbackOnDestroy = function(packet){
        if(!_pool[packet.getType()]){
            _pool[packet.getType()] = new Array();
        }
        _pool[packet.getType()].push(packet);
    }
}
